package com.partvoyage.server.service.facebook;

import java.util.ArrayList;
import java.util.List;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.DefaultJsonMapper;
import com.restfb.DefaultJsonMapper.JsonMappingErrorHandler;
import com.restfb.DefaultWebRequestor;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.Checkin.Place;
import com.restfb.types.Photo;
import com.restfb.types.Post;
import com.restfb.types.User;

/**
 * to get a token.
 * https://developers.facebook.com/tools/explorer?method=GET&path=688971545
 * 
 * @author ludovic
 * 
 */
public class FacebookService {
	private FacebookClient facebookClient;
	private static final String ACCESS_TOKEN = "AAACEdEose0cBAEZCS3MlGSW7NxRv8i9STcTws0esJasxDum81hHj2BnbZBCdvWwGMZAZBMM3ZBZCInpilbio8ZAp41v8ELiifkmNmo46AHSIAZDZD";

	public FacebookService() {
		facebookClient = new DefaultFacebookClient(ACCESS_TOKEN,
				new DefaultWebRequestor(), new DefaultJsonMapper(
						new JsonMappingErrorHandler() {

							public boolean handleMappingError(
									String unmappableJson, Class<?> targetType,
									Exception e) {
								// Here you might log the fact that JSON mapping
								// failed.
								System.out.println("Connexion FB impossible :"
										+ e.toString());
								// Returning true tells the mapper to map this
								// JSON as null and
								// keep going.
								// Returning false tells the mapper to throw an
								// exception.
								return true;
							}
						}));
	}

	public User testCallFB() {
		return facebookClient.fetchObject("me", User.class);
	}

	public Connection<Place> searchPhotoFB(String town) {
		return facebookClient.fetchConnection("search", Place.class,
				Parameter.with("q", town), Parameter.with("type", "place"));

	}

	/**
	 * http://developers.facebook.com/docs/reference/fql/
	 */
	public void testQuery1() {
		String query = "select uid2 from friend where uid1 = me()";
		List<FqlUser> users = facebookClient.executeQuery(query, FqlUser.class);

		System.out.println("Users: " + users);
	}

	/**
	 * http://developers.facebook.com/docs/reference/fql/
	 */
	public void testQuery2() {
		String what = "Bergame";
		String query = "SELECT link FROM photo WHERE aid in (select aid from album where (strpos(name, '"
				+ what
				+ "') >= 0 or strpos(description, '"
				+ what
				+ "') >= 0 or strpos(location, '"
				+ what
				+ "') >= 0) and (owner = me() or owner in (select uid2 from friend where uid1 = me()))) "
				+ "or strpos(caption, '"
				+ what
				+ "') >= 0 and (owner = me() or owner in (select uid2 from friend where uid1 = me())))";
		List<FqlOther> users = facebookClient.executeQuery(query,
				FqlOther.class);

		System.out.println("Users: " + users);
	}

	public List<String> getImgFromFriends(String where) {
		String query = "SELECT src_small FROM photo WHERE aid in (select aid from album where (strpos(name, '"
				+ where
				+ "') >= 0 or strpos(description, '"
				+ where
				+ "') >= 0 or strpos(location, '"
				+ where
				+ "') >= 0) and (owner = me() or owner in (select uid2 from friend where uid1 = me()))) "
				+ "or strpos(caption, '"
				+ where
				+ "') >= 0 and aid in (select aid from album where (owner = me() or owner in (select uid2 from friend where uid1 = me()))) limit 20";

		List<FqlOther> users = facebookClient.executeQuery(query,
				FqlOther.class);

		List<String> returnList = new ArrayList<String>();

		for (FqlOther fl : users) {

			System.out.println("fl.getLink(): " + fl.getLink());
			returnList.add(fl.getLink());
		}
		return returnList;
	}
}
